package xxhui.space.newsclient;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import xxhui.space.newsclient.Adapter.SwitchPageAdapter;
import xxhui.space.newsclient.Fragment.ArticleFragment;
import xxhui.space.newsclient.Fragment.MeFragment;
import xxhui.space.newsclient.InterFacez.MeFragmentCallback;
import xxhui.space.newsclient.util.MyApplication;

//主界面，装载两个fragment
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bottom_navigation)
    BottomNavigationView navigationView;
    @BindView(R.id.headlines_viewpager)
    ViewPager viewPager;

    private MeFragmentCallback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initRes();
        initAction();
    }


    private void initRes() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            navigationView.setElevation(20);
        }

        //加载fragment
        List<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new ArticleFragment());
        MeFragment meFragment = new MeFragment();
        callback = meFragment;
        fragmentList.add(meFragment);
        viewPager.setAdapter(new SwitchPageAdapter(getSupportFragmentManager(), fragmentList));
    }


    private void initAction() {
        //给下部导航栏添加监听，并根据当前页数切换ViewPager
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.article:
                        //Toast.makeText(MainActivity.this, "article", Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.me:
                        //Toast.makeText(MainActivity.this, "me", Toast.LENGTH_SHORT).show();
                        if (MyApplication.isLogin()) {
                            viewPager.setCurrentItem(1);
                        } else {
                            //startActivity(new Intent(MainActivity.this,MeLoginActivity.class));
                            startActivityForResult(new Intent(MainActivity.this,MeLoginActivity.class),0);
                        }
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        //给ViewPager添加监听,并根据ViewPager的页数切换下部导航栏当前页数
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(MyApplication.isLogin()){
            viewPager.setCurrentItem(1);
            callback.updateView();
        }else {
            navigationView.getMenu().getItem(0).setChecked(true);
            viewPager.setCurrentItem(0);
        }
    }
}
